﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WordDowngrade
{
    public partial class DocumentsSorter : Form
    {

        CategoryDict dict;
        public DocumentsSorter()
        {
            InitializeComponent();
            dict = new CategoryDict();
        }

        private void addCategory_Click(object sender, EventArgs e)
        {
            newItemAdder catAdder = new newItemAdder("category");
            catAdder.ShowDialog();
            if (catAdder.getSelectedItem() == null)
            {
                return;
            }
           Categories.Items.Add(catAdder.getSelectedItem());
        }

        private void addLabel_Click(object sender, EventArgs e)
        {
            newItemAdder labelAdder = new newItemAdder("label");
            labelAdder.ShowDialog();
            if (labelAdder.getSelectedItem() == null)
            {
                return;
            }
            Lables.Items.Add(labelAdder.getSelectedItem());
            
        }

        private void deleteLabel_Click(object sender, EventArgs e)
        {
           
                Lables.Items.Remove(Lables.SelectedItem);
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if ((Categories.SelectedItem == null) || (Lables.SelectedItem == null))
            {
                return;
            }
            dict.addLabelToCat((String)Categories.SelectedItem, (String)Lables.SelectedItem);
            Categories_SelectedIndexChanged(null, null);
        }

        private void Categories_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Categories.SelectedItem == null)
            {
                return;
            }
            labelsOfCat.Items.Clear();
            String [] labels=dict.getLables((String)Categories.SelectedItem);
            if (labels == null)
            {
                return;
            }
            for (int i = 0; i < labels.Length; i++)
            {
                labelsOfCat.Items.Add(labels[i]);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CategoryDict ansDict = new CategoryDict();
            OpenFileDialog dialog = new OpenFileDialog();
            setDialog(dialog);
            if (dialog.ShowDialog() == DialogResult.OK)
            {




                    Form2 f = new Form2(dialog.FileNames.Length);
                    f.Show();
                    for (int i = 0; i < dialog.FileNames.Length; i++)
                    {
                        f.workingOn(Path.GetFileNameWithoutExtension(dialog.FileNames[i]));
                        Downgrader.searcher(dict,ansDict,dialog.FileNames[i]);
                        f.increaseProgress();
                    }
                    f.Close();
                    answerPrinter(ansDict);
            }
        }

        private void answerPrinter(CategoryDict dictAns)
        {
             String[] cats = dictAns.Categories;
            foreach (String category in cats)
            {
                richTextBox1.Text = richTextBox1.Text + "Category Name: " + category + "\n"; 
                String []labs = dictAns.getLables(category);
                foreach (String d in labs)
                {
                    richTextBox1.Text = richTextBox1.Text + d + "\n";
                }
                richTextBox1.Text = richTextBox1.Text + "----------------" + "\n";

            }
        }

        private void setDialog(OpenFileDialog dialog)
        {
            dialog.Multiselect = true;
            dialog.Title = "Please Choose Files";
            dialog.Filter = "Office 2007 Files (*.docx;*.xslx)|*.docx";
        }


        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
           AboutBox1 f = new AboutBox1();
           f.Show();
        }
    }
}
